package com.wish.financial.controller;

/**
 * @ClassName TJrProductController
 * @Description TODO
 * @Author gaozijie
 * @Date 2022/5/29 10:06
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.base.dto.FinancialDto;
import com.wish.base.entity.JrProduct;
import com.wish.base.service.IJrProductService;
import com.wish.common.core.web.controller.BaseController;
import com.wish.common.core.web.domain.AjaxResult;
import com.wish.common.core.web.page.TableDataInfo;
import com.wish.common.log.annotation.Log;
import com.wish.common.log.enums.BusinessType;
import com.wish.common.security.annotation.PreAuthorize;
import com.wish.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 金融产品Controller
 *
 * @author wish
 * @date 2022-05-27
 */
@RestController
@RequestMapping("/product")
public class TJrProductController extends BaseController
{
    @Autowired
    private IJrProductService iJrProductService;
    /**
     * 查询金融产品列表
     */
    @PreAuthorize(hasPermi = "financial:product:list")
    @GetMapping("list")
    public AjaxResult list(FinancialDto financialDto)
    {
        startPage();
        List<JrProduct> rentingList = iJrProductService.list(new QueryWrapper<JrProduct>().eq("jr_product_type",1).eq("auditing",1));
        List<JrProduct> homeList = iJrProductService.list(new QueryWrapper<JrProduct>().eq("jr_product_type",2).eq("auditing",1));
        List<JrProduct> receivableList = iJrProductService.list(new QueryWrapper<JrProduct>().eq("jr_product_type",3).eq("auditing",1));
        HashMap<Object, Object> map = new HashMap<>();
        map.put("rentingList",rentingList);
        map.put("homeList",homeList);
        map.put("receivableList",receivableList);

        return AjaxResult.success(map);
    }
    @GetMapping("auditingList")
    public TableDataInfo auditingList(JrProduct jrProduct)
    {

        startPage();
        List<JrProduct> list = iJrProductService.list(new QueryWrapper<JrProduct>().eq("auditing",0));
        return getDataTable(list);


    }

    /**
     * 删除金融产品
     */
    @PreAuthorize(hasPermi = "system:product:remove")
    @Log(title = "作废软删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        JrProduct jrProduct = iJrProductService.getById(id);
        jrProduct.setDelFlg(false);
        boolean orUpdate = iJrProductService.saveOrUpdate(jrProduct);

        return AjaxResult.success(orUpdate==true?1:0);
    }





    /**
     * 新增金融产品
     */
    @PreAuthorize(hasPermi = "system:product:add")
    @PostMapping
    public AjaxResult add(@RequestBody JrProduct tJrProduct)
    {
        tJrProduct.setDelFlg(true);
        tJrProduct.setAuditing(0);
        tJrProduct.setCreateUserid(SecurityUtils.getUserId());
        tJrProduct.setCreateTime(LocalDateTime.now());
        tJrProduct.setUpdateUserid(SecurityUtils.getUserId());
        tJrProduct.setUpdateTime(LocalDateTime.now());
        boolean save = iJrProductService.saveOrUpdate(tJrProduct);
        return toAjax(save==true?1:0);
    }

    /**
     * 修改金融产品
     */
    @PreAuthorize(hasPermi = "system:product:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody JrProduct jrProduct)
    {
        jrProduct.setUpdateUserid(SecurityUtils.getUserId());
        jrProduct.setUpdateTime(LocalDateTime.now());
        boolean flag = iJrProductService.saveOrUpdate(jrProduct);
        return toAjax(flag==true?1:0);
    }


}

